//
//  MTAESCBCRotation.m
//  MyDeviceToolsObjc
//
//  Created by nutria on 9/22/25.
//

#import "MTAESCBCRotation.h"
#import <CommonCrypto/CommonCryptor.h>
#import <Security/Security.h>


@implementation MTAESCBCRotation

+ (NSData *)getEncryptionKey {
    NSString *keyString = @"YourSecureKey123";
    NSData *keyData = [keyString dataUsingEncoding:NSUTF8StringEncoding];
    return keyData;
}

+ (NSData *)getEncryptionIV {
    NSString *ivString = @"YourSecureIV1234";
    NSData *ivData = [ivString dataUsingEncoding:NSUTF8StringEncoding];
    return ivData;
}

+ (NSString *)addString:(NSString *)str {
    if (!str) {
        return @"";
    }
    
    NSData *plainData = [str dataUsingEncoding:NSUTF8StringEncoding];
    if (!plainData) {
        return @"";
    }
    
    NSData *keyData = [self getEncryptionKey];
    NSData *ivData = [self getEncryptionIV];
    
    if (!keyData || !ivData || ivData.length != kCCBlockSizeAES128) {
        return @"";
    }
    
    NSUInteger keySize = keyData.length;
    CCOptions keyOptions;
    
    if (keySize == kCCKeySizeAES128) {
        keyOptions = kCCKeySizeAES128;
    } else if (keySize == kCCKeySizeAES192) {
        keyOptions = kCCKeySizeAES192;
    } else if (keySize == kCCKeySizeAES256) {
        keyOptions = kCCKeySizeAES256;
    } else {
        return @"";
    }
    
    size_t outLength = plainData.length + kCCBlockSizeAES128;
    NSMutableData *encryptedData = [NSMutableData dataWithLength:outLength];
    
    CCCryptorStatus cryptStatus = CCCrypt(
        kCCEncrypt,
        kCCAlgorithmAES,
        kCCOptionPKCS7Padding,
        keyData.bytes,
        keyOptions,
        ivData.bytes,
        plainData.bytes,
        plainData.length,
        encryptedData.mutableBytes,
        encryptedData.length,
        &outLength
    );
    
    if (cryptStatus != kCCSuccess) {
        return @"";
    }
    
    [encryptedData setLength:outLength];
    
    return [encryptedData base64EncodedStringWithOptions:0];
}

+ (NSString *)cutString:(NSString *)str {
    if (!str) {
        return @"";
    }
    
    NSData *encryptedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
    if (!encryptedData) {
        return @"";
    }
    
    NSData *keyData = [self getEncryptionKey];
    NSData *ivData = [self getEncryptionIV];
    
    if (!keyData || !ivData || ivData.length != kCCBlockSizeAES128) {
        return @"";
    }
    
    NSUInteger keySize = keyData.length;
    CCOptions keyOptions;
    
    if (keySize == kCCKeySizeAES128) {
        keyOptions = kCCKeySizeAES128;
    } else if (keySize == kCCKeySizeAES192) {
        keyOptions = kCCKeySizeAES192;
    } else if (keySize == kCCKeySizeAES256) {
        keyOptions = kCCKeySizeAES256;
    } else {
        return @"";
    }
    
    size_t outLength = encryptedData.length;
    NSMutableData *decryptedData = [NSMutableData dataWithLength:outLength];
    
    CCCryptorStatus cryptStatus = CCCrypt(
        kCCDecrypt,
        kCCAlgorithmAES,
        kCCOptionPKCS7Padding,
        keyData.bytes,
        keyOptions,
        ivData.bytes,
        encryptedData.bytes,
        encryptedData.length,
        decryptedData.mutableBytes,
        decryptedData.length,
        &outLength
    );
    
    if (cryptStatus != kCCSuccess) {
        return @"";
    }
    
    [decryptedData setLength:outLength];
    
    NSString *decryptedText = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
    return decryptedText ?: @"";
}


@end
